/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package src;

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 *
 * @author administrador
 */
public class GetSourceCodeFromHTML extends JFrame
{

    boolean entrouInScript = false;

    public GetSourceCodeFromHTML( String what )
    {
        setDefaultCloseOperation( HIDE_ON_CLOSE );

        try
        {
            setTitle( "Teste de source" );
            setSize( new java.awt.Dimension( 640, 480 ) );
            URL url = new URL( "http://www.google.com/dictionary?langpair=en|en&q=" + what + "&hl=pt-BR&aq=f" );
            Degub.getInstance().Debug( "Opening " + "http://www.google.com/dictionary?langpair=en|en&q=" + what + "&hl=pt-BR&aq=f" );
            java.io.InputStream is = url.openStream();
            InputStreamReader isr = new InputStreamReader( is );
            BufferedReader br = new BufferedReader( isr );
            String linha = br.readLine();
            StringBuffer f = new StringBuffer();
            FileOutputStream fout;
            fout = new FileOutputStream( "teste.html" );
            while ( linha != null )
            {
                if ( linha.contains( "Encontrado no dicionário: " ) || entrouInScript )
                {
                    if ( linha.contains( "<div class" ) && linha.contains( "</div>" ) )
                    {
                    }
                    else
                    {
                        entrouInScript = true;
                        f.append( linha + "\n" );
                        new PrintStream( fout ).println( linha + "\n" );
                    }
                }
                linha = br.readLine();
            }

            fout.close();

            JEditorPane o = new JEditorPane( "file:teste.html" );

            JScrollPane scroll = new JScrollPane( o );

            add( scroll );

            Degub.getInstance().Debug( "Trying to show word information " + o.getText().length() );

            if ( o.getText().length() <= 100 )
            {
                o.setText( "For this time we just have support to english words, verify if this word (" + what + ")  is typed correctly and try again " );
            }
            o.setBackground( new Color( 250, 250, 250 ) );
            Font fon = new Font( "Verdana", 0, 11 );
            o.setFont( fon );
            o.setEditable( false );
            setVisible( true );
        }
        catch ( IOException ex )
        {
            Degub.getInstance().Debug( ex );
        }

    }

    public static void main( String args[] ) throws MalformedURLException, IOException
    {
        {
            new GetSourceCodeFromHTML( "notebook" );
        }

    }
}
